#!/usr/bin/python
import time
import subprocess
import sys

from control import *

#setting for rpc
#workaround ipython sys.argv behavior
args = 'control'
hardware_settings_load()
(s, d) = debug_connection_start()
d = ExceptionWrapper(d, rpc_exc_handler)
serial_rpc_reset(s)

#acs setup and wait
#d.portd_set(0xff)
#setup
d.acs_setup()
time.sleep(2)

#processing.jpg 

#num settings
LIGHT_C=10
LIGHT_E=11
LIGHT_F=12

count_GUI=1


#Macro defining
WAITING_TIME=1.5
DO_TIMES=20

d.led_green_set(0)
d.led_red_set(0)
d.led_white_set(0)

"""
#fan testing
print " fan testing"
d.fan_set(1)
time.sleep(2)
d.fan_set(0)
time.sleep(2)
"""

"""
#led testing
print "testing"
print "WHITE"
d.led_white_set(1)
time.sleep(2)
d.led_white_set(0)
print "RED"
d.led_red_set(1)
time.sleep(2)
d.led_red_set(0)
print "GREEN"
d.led_green_set(1)
time.sleep(2)
d.led_green_set(0)
"""

#start checking
while 1:

    print
    print "No."+str(count_GUI)

    #start
    #d.num_set(count_GUI%10)

    ##################################################
    #white led on
    d.led_white_set(1)
    time.sleep(WAITING_TIME)

    #catch the image from webcam
    now_doing="webcam photo captured"
    print "[start]  : " + now_doing
    subprocess.call("fswebcam --config fswebcam.config cam_in.jpg",shell=True)#add cam_in.jpg
    subprocess.call("cp cam_in.jpg processing.jpg",shell=True)
    print "[done]   : " + now_doing

    #white led off
    d.led_white_set(0)
    print "white led is off"

    #modify the webcam input error
    now_doing="modify"
    print "[start]  : " + now_doing
    subprocess.call("./modify/modify processing.jpg",shell=True)
    subprocess.call("cp modify_out.jpg processing.jpg",shell=True)
    print "[donw]   : " + now_doing


    #rotate the image
    now_doing="rotate 180 degrees"
    print "[start]  : " + now_doing
    subprocess.call("convert -rotate 180 processing.jpg cam_rotated.jpg",shell=True)
    subprocess.call("cp cam_rotated.jpg processing.jpg",shell=True)
    #subprocess.call("cp cam_rotated.jpg "+save_path+"white_"+str(count_GUI)+".jpg",shell=True)
    print "[done]   : " + now_doing

    ##################################################

    """
    d.led_green_set(1)
    time.sleep(WAITING_TIME)

    #catch the image from webcam
    now_doing="webcam photo captured"
    print "[start]  : " + now_doing
    subprocess.call("fswebcam -d /dev/video1 -r 1280x1024 cam_in.jpg",shell=True)#add cam_in.jpg
    subprocess.call("cp cam_in.jpg processing.jpg",shell=True)
    print "[done]   : " + now_doing

    d.led_green_set(0)
    print "green led is off"

    #modify the webcam input error
    now_doing="modify"
    print "[start]  : " + now_doing
    subprocess.call("./modify/modify processing.jpg",shell=True)
    subprocess.call("cp modify_out.jpg processing.jpg",shell=True)
    print "[donw]   : " + now_doing


    #rotate the image
    now_doing="rotate 180 degrees"
    print "[start]  : " + now_doing
    subprocess.call("convert -rotate 180 processing.jpg cam_rotated.jpg",shell=True)
    subprocess.call("cp cam_rotated.jpg processing.jpg",shell=True)
    subprocess.call("cp cam_rotated.jpg "+save_path+"green_"+str(count_GUI)+".jpg",shell=True)
    print "[done]   : " + now_doing
    """

    ################################################## 

    #red led on
    d.led_red_set(1)
    time.sleep(WAITING_TIME)

    #catch the image from webcam
    now_doing="webcam photo captured"
    print "[start]  : " + now_doing
    subprocess.call("fswebcam --config fswebcam.config -r 1280x1024 cam_in.jpg",shell=True)#add cam_in.jpg
    subprocess.call("cp cam_in.jpg processing.jpg",shell=True)
    print "[done]   : " + now_doing

    #red led off
    d.led_red_set(0)
    print "Red led is off"

    #modify the webcam input error
    now_doing="modify"
    print "[start]  : " + now_doing
    subprocess.call("./modify/modify processing.jpg",shell=True)
    subprocess.call("cp modify_out.jpg processing.jpg",shell=True)
    print "[donw]   : " + now_doing


    #rotate the image
    now_doing="rotate 180 degrees"
    print "[start]  : " + now_doing
    subprocess.call("convert -rotate 180 processing.jpg cam_rotated.jpg",shell=True)
    subprocess.call("cp cam_rotated.jpg processing.jpg",shell=True)
    #subprocess.call("cp cam_rotated.jpg "+save_path+"red_"+str(count_GUI)+".jpg",shell=True)
    print "[done]   : " + now_doing

    ##############################################

    # # #
    #main part 
    #insert award check program
    subprocess.call("./auto_check_one processing.jpg",shell=True)

    #to see the inside ion
    d.led_white_set(1)

    #acs throw
    now_doing="throw"
    print "[start]  : " + now_doing
    d.fan_set(1)
    time.sleep(1)
    d.acs_throw_one()
    d.fan_set(0)
    time.sleep(2)
    d.acs_throw_two()
    time.sleep(0.5)
    #subprocess.call("echo " +now_doing + "| festival --tts", shell=True)
    print "[done]   : " + now_doing

    #finish
    #d.num_set(LIGHT_C)

    count_GUI+=1

#endind signal
subprocess.call("echo end" + "| festival --tts", shell=True)
d.acs_end()

print "It's finish, Heron.\n"
